<template>
  <div class="uploadImg">
    <el-upload
      action="https://jsonplaceholder.typicode.com/posts/"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :on-success="uploadSuccess"
      :class="{ disabled: isDisable }"
      :on-change="changefile"
      :http-request="upload"
      :before-upload="beforeAvatarUpload"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
    <!-- 进度条 -->
    <el-progress
      :percentage="percentage"
      style="width:180px"
      v-if="showpercentarge"
    ></el-progress>
  </div>
</template>

<script>
/**
 * 密钥
 *  SecretId: AKIDMXS7VcT2PaXCahX7QSEjywd3C4g9LGtF

 *   SecretKey: 7xXqtfAObaIjtmOilZ6ML2TdaGfneEYc
*/

// 导入cos
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDMXS7VcT2PaXCahX7QSEjywd3C4g9LGtF', // 身份识别 ID
  SecretKey: '7xXqtfAObaIjtmOilZ6ML2TdaGfneEYc' // 身份密钥
})

export default {
  data () {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [],
      //进度条
      percentage: 0,
      showpercentarge: false
    }
  },
  methods: {
    handleRemove (file, fileList) {
      this.fileList = []
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    changefile (file, fileList) {
      this.fileList = fileList
    },
    // 上传前的检查,要返回一个boolen类型的值,为true执行上传,为false则不再上传
    beforeAvatarUpload (file) {
      let type = ['image/jpeg', 'image/png', 'image/gif']
      // console.log(type.indexOf(file.type) == -1)
      if (type.indexOf(file.type) == -1) {
        this.$message.error('请上传jpg,png,gif类型')
        return false
      } else if (file.size > 5 * 1024 * 1024) {
        //  文件不能大于5M
        this.$message.error('请选择5M一下的图片')
        return false
      } else {
        return true
      }
    },
    // 用于覆盖的自定义上传,action 可以改为#
    upload (pamart) {
      this.showpercentarge = true
      cos.putObject(
        {
          Bucket: 'dinosaur-1301047310' /* 必须 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: pamart.file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: pamart.file, // 上传文件对象
          onProgress: progressData => {
            // console.log(JSON.stringify(progressData))
            this.percentage = progressData.percent * 100
          }
        },
        (err, data) => {
          //   console.log(err || data)
          //   console.log(data.Location)
          //   成功后把返回的地址作为预览图片的地址 并且要在前面拼接上http://
          this.fileList = [{ url: 'http://' + data.Location }]
          // console.log(this.fileList)
          this.showpercentarge = false
        }
      )
    },
    uploadSuccess () {}
  },
  computed: {
    isDisable () {
      return this.fileList.length > 0
    }
  }
}
</script>

<style lang="scss" scoped>
.disabled {
  ::v-deep .el-upload--picture-card {
    display: none;
  }
}
</style>
